using JT808Gateway.Configs;
using JT808Gateway.Forwarders;
using JT808Protocol.Abstracts;
using JT808Protocol.Handlers;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using System;
using System.Text;
using System.Threading.Tasks;

namespace JT808Gateway
{
    internal class Program
    {
        private static async Task Main(string[] args)
        {
            //注册编码
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

            var config = new ConfigurationBuilder().AddJsonFile("Configs/appSettings.json").Build();
            var kafkaConfig = config.GetSection("KafkaConfig").Get<KafkaConfig>();
            var ip = config.GetValue<string>("GatewayServerConfig:IP");
            var port = config.GetValue<int>("GatewayServerConfig:Port");

            var attachmentServerIP = config.GetValue<string>("AttachmentServerConfig:IP");
            var attachmentServerPort = config.GetValue<int>("AttachmentServerConfig:Port");

            Console.WriteLine($"JT808 Gateway Listening on {ip}:{port}.");

            MsgHandlerBase.AlarmAttachmentServerIP = attachmentServerIP;
            MsgHandlerBase.AlarmAttachmentServerPort = attachmentServerPort;

            var hostBuilder = new JT808GatewayHostBuilder()
                .SetHost(ip)
                .SetPort(port)
                .SetForwaders(forwarders =>
                {
                    //添加转发器
                    forwarders.Add(new KafkaForwarder(kafkaConfig));
                    forwarders.Add(new TcpForwarder());
                    forwarders.Add(new UdpForwarder());
                })
                .SetMeesageHandlers(msgHandlers =>
                {
                    //添加处理器
                    msgHandlers.AddRange(JT808HandlerProvider.GetHandlers());
                });
            var host = hostBuilder.Build();

            //启动网关
            await host.RunAsync();
        }
    }
}